package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;
    @RequestMapping("findAllPerm")
    public Result findAllPerm(){
        try {
            List<Permission> list = permissionService.findAllPerm();
            return new Result(true,"",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DATA_EXCEPTION);
        }
    }
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
        if (queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0) {
            queryPageBean.setQueryString(queryPageBean.getQueryString().trim());// 去除查询条件首尾空格
        }
        return permissionService.findByPage(queryPageBean);
    }
    /**
     * 添加
     * @param permission
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestBody Permission permission) {
        return permissionService.add(permission);
    }

    /**
     * 编辑
     * @param permission
     * @return
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody Permission permission) {
        return permissionService.edit(permission);
    }

    /**
     * 回显权限
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findByID(Integer id) {
        Result result = permissionService.findByID(id);
        return result;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public Result delete(Integer id) {
        return permissionService.delete(id);
    }
}
